// 版本新增页
import { Col, message, Row } from 'antd';
import { connect } from 'umi';
import cls from 'classnames';
import { VersionUpdateContext } from '../common/UpdateContext';
import { UpdateViewLeft } from './UpdateViewLeft';
import '../common/styles.less';
import { VersionManageAPI } from '../common/type';
import { IActionEnum, useUpdateReducer } from '../common/UpdateReducer';
import { UpdateViewRight } from './UpdateViewRight';
import { getVersionDetailService } from '../services';
import { useEffect, useRef } from 'react';

const VersionUpdateView: React.FC<VersionManageAPI.IVersionUpdateView> = ({
  curRes,
  locationProps,
}) => {
  const { dispatch, leftVersionType } = useUpdateReducer();

  /**初始化版本业务标签类名前缀 */
  const prefixCls = 'version-update-';

  /**左侧选中版本 右侧同步更新 */
  const versionTypeRef = useRef<Record<string, any>>();

  const versionTypeEvent = () => {
    versionTypeRef?.current?.getVersionType();
  };

  /**
   * 系统版本详情
   * @param res
   */
  const getVersionByIDCall = (res: any) => {
    if (res && res?.msgFlag) {
      // message.success(res?.msg);
    } else {
      message.error(res?.msg);
    }
  };

  /**调用系统版本详情 */
  const {
    getVersionDetailRun,
    getVersionDetailLoading,
    getVersionDetailData,
  } = getVersionDetailService(getVersionByIDCall);

  useEffect(() => {
    if (locationProps?.state) {
      reloadVersionInfo();
    } else {
      dispatch({
        type: IActionEnum.LEFT_VERSIONTYPE,
        payload: '2',
      });
    }
  }, [locationProps?.state]);

  /**
   * 刷新详情
   */
  const reloadVersionInfo = () => {
    getVersionDetailRun(locationProps?.state);
  };

  return (
    <VersionUpdateContext.Provider
      value={{
        prefixCls,
        curRes,
        dispatch, //派发
        leftVersionType,
        locationProps,

        reloadVersionInfo, //刷新详情
        getVersionDetailData, //详情数据
      }}
    >
      <Row className={cls(prefixCls + 'row')} gutter={[8, 0]}>
        <Col
          span={16}
          className={cls(prefixCls + 'row-col')}
          style={{ paddingLeft: '0px' }}
        >
          <UpdateViewLeft
            getVersionLoading={getVersionDetailLoading}
            versionTypeEvent={versionTypeEvent}
          />
        </Col>
        <Col
          span={8}
          className={cls(prefixCls + 'row-col')}
          style={{ paddingRight: '0px' }}
        >
          <UpdateViewRight ref={versionTypeRef} />
        </Col>
      </Row>
    </VersionUpdateContext.Provider>
  );
};

export default connect(({ baseLogin, navTab }: Record<string, any>) => ({
  curRes: baseLogin?.permissions,
  navTab,
}))(VersionUpdateView);
